﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;

namespace Marmat.Forms.Skin
{
    /// <summary>
    /// 基于 Image 的扩展实例
    /// </summary>
    public static class ImageExtension
    {

        /// <summary>
        /// 将图片剪裁成椭圆形图像。
        /// </summary>
        /// <param name="img">原始Image对象</param>
        /// <returns></returns>
        public static Image CutEllipse(this Image img)
        {
            return CutEllipse(img, new Rectangle(0, 0, img.Width, img.Height), img.Size);
        }

        /// <summary>
        /// 将图片剪裁成椭圆形图像。
        /// </summary>
        /// <param name="img">原始Image对象</param>
        /// <param name="rect">被切割图片的矩形范围</param>
        /// <param name="size">椭圆的尺寸</param>
        /// <returns></returns>
        public static Image CutEllipse(this Image img, Rectangle rect, Size size)
        {
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (TextureBrush br = new TextureBrush(img, WrapMode.Clamp, rect))
                {
                    br.ScaleTransform(bitmap.Width / (float)rect.Width, bitmap.Height / (float)rect.Height);
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    g.FillEllipse(br, new Rectangle(0, 0, size.Width, size.Height));
                }
            }
            return bitmap;
        }
    }
}
